package com.cztv.vote.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.cztv.pub.cache.Cache;
import com.cztv.pub.cache.CacheManager;
import com.cztv.vote.bean.ReturnValBean;
import com.cztv.vote.bean.VoteProjectBean;
import com.cztv.vote.bean.VoteRecord;
import com.cztv.vote.dao.VoteDao;

/**
 * 
 * 
 * @author ct
 *
 */
@Transactional
public class VoteManager {
	private VoteDao voteDao;
	@Required  
	public void setVoteDao(VoteDao voteDao) {
		this.voteDao = voteDao;
	}
	
	public void vote(VoteRecord record){
		voteDao.insert(record);
	}
	
	public void updateVotes(){
		boolean isNeedUpdate=false;
		
		Map<Integer,Integer> addCounts=new HashMap<Integer,Integer>();
		Map<Integer,Integer> oldCounts=new HashMap<Integer,Integer>();
		
		
		addCounts=voteDao.countByGroup();
		
		if(addCounts.size()>0){
			oldCounts=voteDao.countByIds();
			for(Map.Entry<Integer,Integer> es:addCounts.entrySet()){
				int sum=oldCounts.get(es.getKey())+es.getValue();
				voteDao.updateVotes(sum, es.getKey());
			}
			
			isNeedUpdate=true;
		}
		
	
		if(isNeedUpdate)
			voteDao.updateLogs();
		
	}
	
	public VoteProjectBean getVoteProjectInfoFromDB(int rid){
		Object bean=voteDao.queryById(rid);
		
		return bean==null?null:(VoteProjectBean)bean;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<VoteProjectBean> queryAllProjects(){
		return voteDao.queryAll();
	}
	
	private VoteProjectBean getVoteProjectInfoFromCache(int rid){
		System.out.println("rid="+rid);
		Object bean= CacheManager.getInstance().getContent(String.valueOf(rid));
		if(bean!=null){
			Cache cache=(Cache)bean;
			return (VoteProjectBean) cache.getValue();
		}
		return null;
	}
	
	private int getVotes(int rid){
		return voteDao.getCountById(rid);
	}
	
	public ReturnValBean voteCheck(int rid){
		ReturnValBean val=new ReturnValBean();
		
		val.setBoolVal(false);
		
		VoteProjectBean bean=getVoteProjectInfoFromCache(rid);
		if(bean==null){
			val.setInfo("投票项目不存在");
			return val;
		}
			
		//判断投票项目是否打开
		if(!bean.isIspOpen()){
			val.setInfo("该投票已经关闭");
			return val;
		}
			
		long nowTime=System.currentTimeMillis()/1000;
		//判断投票是否正在进行中
		if(bean.getEndtime()<nowTime || bean.getRegtime()>nowTime){
			val.setInfo("该投票已过期");
			return val;
		}
			
		//是否限制网络票数，判断票数是否超过最大值
		if(bean.isVoteNum()){
			if(bean.getVoteNumLimit()<getVotes(rid)){
				val.setInfo("该投票数已经达到最大限制");
				return val;
			}
		}
		
		val.setBoolVal(true);
		val.setInfo("投票成功");
		
		return val;
	}
	
	public ReturnValBean isEnableVote(String ip,int rid){
		VoteProjectBean bean=getVoteProjectInfoFromCache(rid);
		ReturnValBean val=new ReturnValBean();
		
		if(bean.isIp()){
			int count=voteDao.queryCountByIp(ip);
			if(count!=0){
				long voteTime=voteDao.queryTimeByIp(ip);
				
				long limitTime=bean.getIpTimeLimit();
				
				long nowTime=System.currentTimeMillis();
				
				if(voteTime+limitTime<nowTime){
					val.setBoolVal(false);
					val.setInfo("不能在短时间内投票多次");
					return val;
				}
					
			}
		}
			
		val.setBoolVal(true);
		val.setInfo("ip验证通过");
		
		return val;
	}
	
}
